package cs554.androidkids;

import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
	private Context mContext;
	// position of the blank cell
	public int blankPos;

	public ImageAdapter(Context c) {
		mContext = c;
		gridImgs = Arrays.asList(R.drawable.grid_1, R.drawable.grid_2,
				R.drawable.grid_3, R.drawable.grid_4, R.drawable.grid_5,
				R.drawable.grid_6, R.drawable.grid_7, R.drawable.grid_8,
				R.drawable.blank);
	}

	public int getCount() {
		return gridImgs.size();
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	// create a new ImageView for each item referenced by the Adapter
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView;
		if (convertView == null) { // if it's not recycled, initialize some
									// attributes
			imageView = new ImageView(mContext);
			imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			imageView.setPadding(8, 8, 8, 8);
		} else {
			imageView = (ImageView) convertView;
		}

		imageView.setImageResource(gridImgs.get(position));
		Log.i(Utils.TAG, "creating view at position " + position + ", widget "
				+ imageView);
		if (gridImgs.get(position) == R.drawable.blank)
			this.blankPos = position;
		return imageView;
	}

	void swap(int position) {
		Integer temp = gridImgs.get(position);
		gridImgs.set(position, gridImgs.get(blankPos));
		gridImgs.set(blankPos, temp);

		blankPos = position;
	}

	// references to our images
	private List<Integer> gridImgs;
}
